package casino;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.util.concurrent.CopyOnWriteArrayList;

public class MenuList extends MenuItem {
	private Rectangle inner;
	private int padding;
	protected CopyOnWriteArrayList<String> elements;
	private int bHeight;
	protected int cur = -1;
	private TextLayout layout;
	private Color highlight = new Color(0,0, 100, 100);
	private BufferedImage buffer;
	private Graphics2D bufGraphics;
	private ScrollBar scroll;

	public MenuList(Menu m, int x, int y, int w, int h, int p, CopyOnWriteArrayList<String> e, int f, int bh){
		super(m,x,y,w,h,"",f);
		inner = new Rectangle(p,p,w-2*p,h-2*p);
		padding = p;
		elements = e;
		bHeight = bh;
		buffer = new BufferedImage(width+1, height+1, BufferedImage.TYPE_INT_RGB);
		scroll = new ScrollBar(host, x+width-padding-25, y+padding, height-2*padding, 1000);
		init();
	}
	
	public void init(){}
	
	public MenuList(Menu m, int x, int y, int w, int h, int p, int f, int bh){
		super(m,x,y,w,h,"",f);
		inner = new Rectangle(p,p,w-2*p,h-2*p);
		padding = p;
		elements = new CopyOnWriteArrayList<String>();
		bHeight = bh;
		buffer = new BufferedImage(width+1, height+1, BufferedImage.TYPE_INT_RGB);
		scroll = new ScrollBar(host, x+width-padding-25, y+padding, height-2*padding, 0);
		init();
	}
	
	public void update(){
		super.update();
		scroll.update();
	}
	
	public void clicked(MouseEvent e){
		if(scroll.contains(e.getX(), e.getY())){
			scroll.mousePress(e);
		}
	}
	
	public void released(MouseEvent e){
		scroll.mouseRelease(e);
	}
	
	public void onHover(int x, int y){
		if(inner.contains(x-this.x,y-this.y)){
			cur = (y-this.y-inner.y+scroll.offset())/bHeight;
			if(cur >= elements.size()){
				cur = -1;
			}
		}else{
			cur = -1;
		}
	}
	
	public void offHover(int x, int y){
		cur = -1;
	}
	
	@Override
	protected void draw(Graphics2D g){
		bufGraphics = (Graphics2D)buffer.getGraphics();
		bufGraphics.setColor(Color.gray.brighter());
		bufGraphics.fillRect(inner.x, inner.y, inner.width, inner.height);
		if(cur >= 0){
			bufGraphics.setColor(highlight);
			bufGraphics.fillRect(inner.x, cur*bHeight+inner.y-scroll.offset(), inner.width, bHeight);
		}
		bufGraphics.setColor(Color.black);
		bufGraphics.setFont(font);
		for(int i = 0; i < elements.size(); i++){
			layout = new TextLayout(elements.get(i), font, bufGraphics.getFontRenderContext());
			bufGraphics.drawString(elements.get(i), inner.x+20, i*bHeight+inner.y+(int)(bHeight+layout.getBounds().getHeight())/2-scroll.offset());
		}
		bufGraphics.setColor(Color.gray);
		bufGraphics.fillRect(0, 0, padding, height);
		bufGraphics.fillRect(width-padding, 0, padding, height);
		bufGraphics.fillRect(padding, 0, width-2*padding, padding);
		bufGraphics.fillRect(padding, height-padding, width-2*padding, padding);
		bufGraphics.setColor(Color.black);
		bufGraphics.drawRect(0, 0, width, height);
		bufGraphics.drawRect(inner.x, inner.y, inner.width, inner.height);
		g.drawImage(buffer, x, y,host.casino);
		scroll.draw(g);
	}
	
	public void add(String s){
		elements.add(s);
		scroll.setPaneHeight(elements.size()*bHeight);
	}
	
	public void remove(String s){
		elements.remove(s);
		scroll.setPaneHeight(elements.size()*bHeight);
	}
	
	public void remove(int i){
		elements.remove(i);
		scroll.setPaneHeight(elements.size()*bHeight);
	}

}
